﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CommonMethodServiceRef;
    using WMS6.Web.OutboundService2Ref;

    public class ShippingReview : BasePage
    {
        protected Button btnConfirm;
        protected Button btnRefresh;
        protected Button btnUpdateScanQty;
        protected DropDownList ddlOrderType;
        protected DropDownList ddlUomID;
        protected HtmlForm F7081;
        protected GridView gvOutboundReview;
        protected GridView gvOutboundReviewDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnGvCss;
        protected HtmlInputHidden hdnGvDCss;
        protected HtmlInputHidden hdnIndex;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOrderLineID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlInputHidden hdnWHID;
        protected Label lblASN;
        protected Label lblBarcode;
        protected Label lblCaseID;
        protected Label lblMsg;
        protected Label lblOrderCount;
        protected Label lblOrderDetailCount;
        protected Label lblOrderID;
        protected Label lblOrdersID;
        protected Label lblOrdersQty;
        protected Label lblOrderType;
        protected Label lblPickedQty;
        protected Label lblQty;
        protected Label lblReviewQty;
        protected Label lblSkuDesc;
        protected Label lblSkuSytleCount;
        protected Label lblSMQty;
        protected Label lblStatus;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected HtmlImage SkuImg;
        protected TextBox txtBarcode;
        protected TextBox txtCaseID;
        protected TextBox txtOrderCount;
        protected TextBox txtOrderDetailQty;
        protected TextBox txtOrderID;
        protected TextBox txtOrdersID;
        protected TextBox txtOrdersQty;
        protected TextBox txtPickedQty;
        protected TextBox txtQty;
        protected TextBox txtReviewQty;
        protected TextBox txtSkuDesc;
        protected TextBox txtSkuStyleCount;
        protected TextBox txtSMQty;
        protected UpdatePanel UpdatePanel1;

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string strtemp = string.Empty;
            try
            {
                if (this.ViewState["ShippingReviewList"] != null)
                {
                    List<OrdersInfo> ordersList = this.ViewState["ShippingReviewList"] as List<OrdersInfo>;
                    foreach (OrdersInfo info in ordersList)
                    {
                        if (info.TotalQty != info.ReviewQty)
                        {
                            this.lblMsg.Text = "单据" + info.OrderID + "复核数量与拣货数量不相符合,无法确认";
                            return;
                        }
                    }
                    strtemp = OutboundService2ClientProxy.GetInstance().ConfirmShippingReviewByOrderID(ordersList);
                    this.lblMsg.Text = strtemp + "确认成功!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnUpdateScanQty_Click(object sender, EventArgs e)
        {
            this.txtSMQty.Enabled = true;
        }

        protected void gvOutboundReview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (string.IsNullOrEmpty(((Label) e.Row.FindControl("lblOrderID")).Text) || (((Label) e.Row.FindControl("lblOrderID")).Text == "&nbsp;"))
                {
                    ((LinkButton) e.Row.FindControl("lnkbtnSelectOwner")).Visible = false;
                    ((Image) e.Row.FindControl("StatusImg")).Visible = false;
                    ((Label) e.Row.FindControl("lblSkuGroupCount")).Text = "";
                    ((Label) e.Row.FindControl("lblTotalQty")).Text = "";
                    ((Label) e.Row.FindControl("lblReviewQty")).Text = "";
                }
                if (!string.IsNullOrEmpty(((Label) e.Row.FindControl("lblReviewQty")).Text) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblTotalQty")).Text))
                {
                    if (decimal.Parse(((Label) e.Row.FindControl("lblReviewQty")).Text) == decimal.Parse(((Label) e.Row.FindControl("lblTotalQty")).Text))
                    {
                        ((Image) e.Row.FindControl("StatusImg")).ImageUrl = "~/Images/right.gif";
                    }
                    else
                    {
                        ((Image) e.Row.FindControl("StatusImg")).ImageUrl = "~/Images/wrong.gif";
                    }
                }
            }
        }

        protected void gvOutboundReview_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {
                if (this.ViewState["ShippingReviewList"] != null)
                {
                    this.hdnIndex.Value = e.NewSelectedIndex.ToString();
                    string orderID = this.gvOutboundReview.DataKeys[e.NewSelectedIndex]["OrderID"].ToString();
                    List<OrdersInfo> orderList = this.ViewState["ShippingReviewList"] as List<OrdersInfo>;
                    if (orderList != null)
                    {
                        int index = 0;
                        foreach (OrdersInfo info in orderList)
                        {
                            if (info.OrderID == orderID)
                            {
                                this.hdnOrderID.Value = orderID;
                                this.txtOrdersID.Text = orderID;
                                this.ViewState["ShippingDetailReviewList"] = info.OrderDetailInfos;
                                this.SetGridViewCss(this.gvOutboundReview, index);
                                base.BindData2GridView(this.gvOutboundReviewDetail, this.SetDetailGvidViewRow(info.OrderDetailInfos, 5));
                                this.SetGridViewCss(this.gvOutboundReviewDetail, 0);
                            }
                            index++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                this.txtBarcode.Text = "";
                this.txtBarcode.Focus();
            }
        }

        protected void gvOutboundReviewDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                if (string.IsNullOrEmpty(((Label) e.Row.FindControl("lblSkuID")).Text) || (((Label) e.Row.FindControl("lblSkuID")).Text == "&nbsp;"))
                {
                    ((Image) e.Row.FindControl("StatusImg")).Visible = false;
                    ((Label) e.Row.FindControl("lblPickedQty")).Text = "";
                    ((Label) e.Row.FindControl("lblReviewQty")).Text = "";
                }
                if (!string.IsNullOrEmpty(((Label) e.Row.FindControl("lblPickedQty")).Text) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblReviewQty")).Text))
                {
                    if (decimal.Parse(((Label) e.Row.FindControl("lblPickedQty")).Text) == decimal.Parse(((Label) e.Row.FindControl("lblReviewQty")).Text))
                    {
                        ((Image) e.Row.FindControl("StatusImg")).ImageUrl = "~/Images/right.gif";
                    }
                    else
                    {
                        ((Image) e.Row.FindControl("StatusImg")).ImageUrl = "~/Images/wrong.gif";
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.ViewState["ShippingReviewList"] = new List<OrdersInfo>();
                base.BindData2GridView(this.gvOutboundReview, this.SetGvidViewRow(new List<OrdersInfo>(), 10));
                base.BindData2GridView(this.gvOutboundReviewDetail, this.SetDetailGvidViewRow(new List<OrderDetailInfo>(), 5));
                this.ViewState["ShippingDetailReviewList"] = new List<OrderDetailInfo>();
                base.BindData2DDLBySysCode(this.ddlOrderType, "ShippingReviewType");
                this.txtOrderID.Focus();
            }
            this.lblMsg.Text = "";
        }

        private List<OrderDetailInfo> SetDetailGvidViewRow(List<OrderDetailInfo> orderDetailInfoList, int rowCount)
        {
            if (orderDetailInfoList == null)
            {
                orderDetailInfoList = new List<OrderDetailInfo>();
            }
            for (int i = orderDetailInfoList.Count<OrderDetailInfo>(); i < rowCount; i++)
            {
                OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
                orderDetailInfoList.Add(orderDetailInfo);
            }
            return orderDetailInfoList;
        }

        private void SetGridViewCss(GridView gv, int index)
        {
            for (int i = 0; gv.Rows.Count > i; i++)
            {
                if (gv.Rows[i].CssClass == "GridViewRowAutoSelectStyle")
                {
                    if (gv.ID == "gvOutboundReview")
                    {
                        gv.Rows[i].CssClass = this.hdnGvCss.Value;
                    }
                    else
                    {
                        gv.Rows[i].CssClass = this.hdnGvDCss.Value;
                    }
                }
                if (i == index)
                {
                    if (gv.ID == "gvOutboundReview")
                    {
                        this.hdnGvCss.Value = gv.Rows[i].CssClass;
                        gv.Rows[i].CssClass = "GridViewRowAutoSelectStyle";
                    }
                    else
                    {
                        this.hdnGvDCss.Value = gv.Rows[i].CssClass;
                        gv.Rows[i].CssClass = "GridViewRowAutoSelectStyle";
                    }
                }
            }
        }

        private List<OrdersInfo> SetGvidViewRow(List<OrdersInfo> ordersInfoList, int rowCount)
        {
            if (ordersInfoList == null)
            {
                ordersInfoList = new List<OrdersInfo>();
            }
            for (int i = ordersInfoList.Count<OrdersInfo>(); i < rowCount; i++)
            {
                OrdersInfo ordersinfo = new OrdersInfo();
                ordersInfoList.Add(ordersinfo);
            }
            return ordersInfoList;
        }

        private void SetOrderDetailDate()
        {
            try
            {
                if ((((this.gvOutboundReview.Rows.Count > 0) && (this.gvOutboundReview.Rows.Count >= int.Parse(this.hdnIndex.Value))) && (this.ViewState["ShippingReviewList"] != null)) && (this.gvOutboundReview.DataKeys[int.Parse(this.hdnIndex.Value)]["OrderID"] != null))
                {
                    string orderID = this.gvOutboundReview.DataKeys[int.Parse(this.hdnIndex.Value)]["OrderID"].ToString();
                    List<OrdersInfo> orderList = this.ViewState["ShippingReviewList"] as List<OrdersInfo>;
                    if (orderList != null)
                    {
                        int index = 0;
                        foreach (OrdersInfo info in orderList)
                        {
                            if (info.OrderID == orderID)
                            {
                                this.txtCaseID.Text = info.CaseID;
                                this.hdnOrderID.Value = orderID;
                                this.txtOrdersID.Text = orderID;
                                this.ViewState["ShippingDetailReviewList"] = info.OrderDetailInfos;
                                base.BindData2GridView(this.gvOutboundReviewDetail, this.SetDetailGvidViewRow(info.OrderDetailInfos, 5));
                                this.SetGridViewCss(this.gvOutboundReviewDetail, index);
                            }
                            index++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                this.txtBarcode.Text = "";
                this.txtBarcode.Focus();
            }
        }

        protected void txtBarcode_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtSMQty.Text))
                {
                    this.lblMsg.Text = "请输入扫描数量！";
                    this.txtSMQty.Focus();
                }
                else if (!string.IsNullOrEmpty(this.txtBarcode.Text))
                {
                    decimal Qty = decimal.Parse(this.txtSMQty.Text);
                    SkuInfo skuInfo = CommoneMethodServiceClientProxy.GetInstance().GetSkuByBarcode(this.txtBarcode.Text);
                    if (skuInfo != null)
                    {
                        this.SkuImg.Src = "../Upload/SkuLogo/" + skuInfo.Udf9;
                        bool isInsert = false;
                        bool IsReview = true;
                        bool IsQty = false;
                        bool isNext = false;
                        if (this.ViewState["ShippingDetailReviewList"] != null)
                        {
                            List<OrderDetailInfo> orderDetailList = this.ViewState["ShippingDetailReviewList"] as List<OrderDetailInfo>;
                            if (orderDetailList != null)
                            {
                                foreach (OrderDetailInfo info in orderDetailList)
                                {
                                    if ((info.OrderID == this.hdnOrderID.Value) && (skuInfo.SkuID.ToUpper() == info.SkuID.ToUpper()))
                                    {
                                        if (info.PickedQty >= (info.ReviewQty + Qty))
                                        {
                                            info.ReviewQty += Qty;
                                            this.txtReviewQty.Text = info.ReviewQty.ToString();
                                            this.hdnOrderID.Value = info.OrderID;
                                            this.hdnOrderLineID.Value = info.LineID.ToString();
                                            this.txtSkuDesc.Text = info.SkuDescr;
                                            this.txtPickedQty.Text = info.PickedQty.ToString();
                                            this.txtOrdersQty.Text = info.OriginalQty.ToString();
                                            this.txtReviewQty.Text = info.ReviewQty.ToString();
                                            isInsert = true;
                                            IsReview = true;
                                            orderDetailList.Remove(info);
                                            orderDetailList.Insert(0, info);
                                            break;
                                        }
                                        if (info.PickedQty < (info.ReviewQty + Qty))
                                        {
                                            IsQty = true;
                                        }
                                        else
                                        {
                                            IsReview = false;
                                        }
                                    }
                                }
                                this.ViewState["ShippingDetailReviewList"] = orderDetailList;
                                base.BindData2GridView(this.gvOutboundReviewDetail, this.SetDetailGvidViewRow(orderDetailList, 5));
                                this.SetGridViewCss(this.gvOutboundReviewDetail, 0);
                                if (this.ViewState["ShippingReviewList"] != null)
                                {
                                    List<OrdersInfo> ordersList = this.ViewState["ShippingReviewList"] as List<OrdersInfo>;
                                    foreach (OrdersInfo info in ordersList)
                                    {
                                        if ((!IsQty && isInsert) && (info.OrderID == this.hdnOrderID.Value))
                                        {
                                            if (info.TotalQty >= (info.ReviewQty + Qty))
                                            {
                                                info.ReviewQty += Qty;
                                                info.OrderDetailInfos = orderDetailList;
                                                if ((info.TotalQty == info.ReviewQty) && (info.IsPrint != "Y"))
                                                {
                                                    info.IsPrint = "Y";
                                                    List<OrdersInfo> ordersInfo = new List<OrdersInfo> {
                                                        info
                                                    };
                                                    OutboundService2ClientProxy.GetInstance().ConfirmShippingReviewByOrderID(ordersInfo);
                                                    this.hdnWHID.Value = base.UserInfo.CurrentWH;
                                                    this.hdnOrderID.Value = info.OrderID;
                                                    this.hdnOwnerID.Value = info.OwnerID;
                                                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>doPrintView();</script>", false);
                                                }
                                                ordersList.Remove(info);
                                                ordersList.Insert(0, info);
                                                break;
                                            }
                                            isNext = true;
                                        }
                                    }
                                    this.ViewState["ShippingReviewList"] = ordersList;
                                    base.BindData2GridView(this.gvOutboundReview, this.SetGvidViewRow(ordersList, 10));
                                    this.SetGridViewCss(this.gvOutboundReview, 0);
                                }
                            }
                        }
                        if (isNext)
                        {
                            this.hdnIndex.Value = (int.Parse(this.hdnIndex.Value) + 1).ToString();
                            this.SetOrderDetailDate();
                        }
                        if (!isInsert)
                        {
                            this.lblMsg.Text = "单据" + this.txtOrderID.Text + ", 没有找到匹配的数据!";
                        }
                        if (IsQty)
                        {
                            this.lblMsg.Text = "货品" + skuInfo.SkuID + ",扫描数量不能大于拣货数量!";
                        }
                        else if (!IsReview)
                        {
                            this.lblMsg.Text = "扫描货品已经复核完成,无法继续复核!";
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = "未找到匹配的货品请检查条码!";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                this.txtBarcode.Text = "";
                this.txtBarcode.Focus();
            }
        }

        protected void txtOrderID_OnTextChanged(object sender, EventArgs e)
        {
            List<OrdersInfo> orderList = null;
            try
            {
                if (!string.IsNullOrEmpty(this.txtOrderID.Text))
                {
                    orderList = OutboundService2ClientProxy.GetInstance().GetShippingReviewByOrderID(this.txtOrderID.Text, this.ddlOrderType.SelectedValue);
                    if ((orderList != null) && (orderList.Count > 0))
                    {
                        int orderDetailCount = 0;
                        List<string> SkuSytleCount = new List<string>();
                        decimal totalQty = 0M;
                        int i = 0;
                        foreach (OrdersInfo info in orderList)
                        {
                            i++;
                            info.CaseID = (i <= 9) ? ("0" + i.ToString()) : i.ToString();
                            orderDetailCount += info.OrderDetailInfos.Count<OrderDetailInfo>();
                            foreach (OrderDetailInfo infodetail in info.OrderDetailInfos)
                            {
                                if (!SkuSytleCount.Contains(infodetail.SkuID))
                                {
                                    SkuSytleCount.Add(infodetail.SkuID);
                                    totalQty += infodetail.PickedQty;
                                }
                            }
                        }
                        this.txtOrderCount.Text = orderList.Count<OrdersInfo>().ToString();
                        this.txtSkuStyleCount.Text = SkuSytleCount.Count<string>().ToString();
                        this.txtQty.Text = totalQty.ToString();
                        this.txtOrderDetailQty.Text = orderDetailCount.ToString();
                        this.hdnIndex.Value = "0";
                        goto Label_01F2;
                    }
                    this.lblMsg.Text = "未找到匹配的数据!";
                    this.txtOrderID.Text = "";
                    this.txtOrderID.Focus();
                }
                return;
            Label_01F2:
                this.ViewState["ShippingReviewList"] = orderList;
                base.BindData2GridView(this.gvOutboundReview, this.SetGvidViewRow(orderList, 10));
                this.SetGridViewCss(this.gvOutboundReview, int.Parse(this.hdnIndex.Value));
                this.SetOrderDetailDate();
                this.txtBarcode.Focus();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

